<template>
  <div>
    <!-- show-checkbox  节点是否可被选择
         check-strictly 取消父节点和子节点的管理
         render-after-expand 是否默认展开
    -->

    <el-tree
      ref="tree"
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
      :data="list"
      :props="{label:'name',children:'children'}"
    />
    <el-row height="60px" type="flex" justify="end">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button type="info" @click="$emit('close')">取消</el-button>
    </el-row>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      list: []

    }
  },
  created() {
    this.loadePermissionList()
  },
  methods: {
    async hSubmit() {
      try {
        // 获取选中的复选框
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.roleId, permIds: permIds })
        console.log('assignPerm', res)
        // 关闭弹层
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('assignPerm', err)
      }
    },
    // 获取列表
    async  loadePermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // transDataToTreeData 转化为树形结构
        this.list = transDataToTreeData(res.data)
        // 发送axios 保存id来获取获取角色详情
        const info = await getRoleDetail(this.roleId)
        console.log(info, 'info')
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
